package com.dianning.service.imp;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.commons.logging.*;

import com.dianning.bean.AdSpace;
import com.dianning.bean.Channel;
import com.dianning.bean.ChannelContact;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.dao.AdAdsDAO;
import com.dianning.dao.AdSpaceDAO;
import com.dianning.dao.ChannelContactDAO;
import com.dianning.dao.ChannelDAO;
import com.dianning.dao.VisitorDAO;
import com.dianning.exception.DianningException;
import com.dianning.service.AdService;
import com.dianning.service.Cache;
import com.dianning.service.ChannelService;
import com.dianning.service.VisitorService;

public class ChannelServiceImp implements ChannelService {

	private static final Log logger = LogFactory.getLog(ChannelServiceImp.class);
	
	private ChannelDAO channelDAO;
	private ChannelContactDAO channelContactDAO;
	private Cache infoChannelCache;

	public ChannelDAO getChannelDAO() {
		return channelDAO;
	}

	public void setChannelDAO(ChannelDAO channelDAO) {
		this.channelDAO = channelDAO;
	}

	public ChannelContactDAO getChannelContactDAO() {
		return channelContactDAO;
	}

	public void setChannelContactDAO(ChannelContactDAO channelContactDAO) {
		this.channelContactDAO = channelContactDAO;
	}
	
	public Cache getInfoChannelCache() {
		return infoChannelCache;
	}

	public void setInfoChannelCache(Cache infoChannelCache) {
		this.infoChannelCache = infoChannelCache;
	}




	/**
	 * 根据主键ID获取Channel对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Channel
	 * 			Channel对象
	 */
	public Channel findChannelById(long id){
		if(Constant.USE_CACHE){
			Channel channel = (Channel) this.getInfoChannelCache().get("[channel]["+id+"]");
			if(channel == null){
				channel = this.getChannelDAO().findChannelById(id);
				this.getInfoChannelCache().add("[channel]["+id+"]",channel);
			}
			return channel;
		}else{
			return this.getChannelDAO().findChannelById(id);
		}
	}
	
	/**
	 * 保存Channel对象
	 * 
	 * @param channel
	 * 			Channel对象
	 * @return Channel
	 * 			Channel对象
	 */
	public Channel updateChannel(Channel channel){
		return this.getChannelDAO().updateChannel(channel);
	}
	
	/**
	 * 根据主键ID获取ChannelContact对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return ChannelContact
	 * 			ChannelContact对象
	 */
	public ChannelContact findChannelContactById(long id){
		if(Constant.USE_CACHE){
			ChannelContact cc = (ChannelContact) this.getInfoChannelCache().get("[channelContact]["+id+"]");
			if(cc == null){
				cc = this.getChannelContactDAO().findChannelContactById(id);
				this.getInfoChannelCache().add("[channelContact]["+id+"]",cc);
			}
			Channel channel = (Channel) this.getInfoChannelCache().get("[channel]["+cc.getChannelId()+"]");
			if(channel == null){
				channel = this.getChannelDAO().findChannelById(cc.getChannelId());
				this.getInfoChannelCache().add("[channel]["+cc.getChannelId()+"]",channel);
			}
			cc.setChannel(channel);
			return cc;
		}else{
			ChannelContact cc = this.getChannelContactDAO().findChannelContactById(id);
			cc.setChannel(this.getChannelDAO().findChannelById(cc.getChannelId()));
			return cc;
		}
	}
	
	public Channel findChannelByCcid(long ccid){
		if(Constant.USE_CACHE){
			ChannelContact cc = (ChannelContact) this.getInfoChannelCache().get("[channelContact]["+ccid+"]");
			if(cc == null){
				cc = this.getChannelContactDAO().findChannelContactById(ccid);
				this.getInfoChannelCache().add("[channelContact]["+ccid+"]",cc);
			}
			Channel channel = (Channel) this.getInfoChannelCache().get("[channel]["+cc.getChannelId()+"]");
			if(channel == null){
				channel = this.getChannelDAO().findChannelById(cc.getChannelId());
				this.getInfoChannelCache().add("[channel]["+cc.getChannelId()+"]",channel);
			}
			return channel;
		}else{
			ChannelContact cc = this.getChannelContactDAO().findChannelContactById(ccid);
			return this.getChannelDAO().findChannelById(cc.getChannelId());
		}
	}
	
	/**
	 * 获取ChannelContact对象列表
	 * @param parentId
	 * @param depth
	 * @param orderby
	 * @param ascOrDesc
	 * @return List
	 */
	public List findChannelContacts(long parentId,int depth,String orderby, int ascOrDesc){
		List l = this.getChannelContactDAO().findChannelContacts(parentId, depth, orderby, ascOrDesc);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			for(int i=0;i<l.size();i++){
				ChannelContact cc = (ChannelContact) l.get(i);
				cc.setChannel(this.getChannelDAO().findChannelById(cc.getChannelId()));
			}
			return l;
		}
	}
	
	/**
	 * 获取Channel列表
	 * 
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @return List
	 * 			Area对象列表
	 */
	public List findChannels(String orderby, int ascOrDesc){
		return this.getChannelDAO().findChannels(orderby, ascOrDesc);
	}
	
	public Channel findChannelByCode(String code){
		return this.getChannelDAO().findChannelByCode(code);
	}

}
